package com.salon.server;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.salon.client.GreetingService;
import com.salon.client.ServiceException;
import com.salon.client.beans.ClientBean;
import com.salon.client.beans.IncomeBean;
import com.salon.client.beans.IncomeMaterialsBean;
import com.salon.client.beans.MasterBean;
import com.salon.client.beans.MaterialBean;
import com.salon.client.beans.ParentPagingLoadResult;
import com.salon.client.beans.PriceBean;
import com.salon.client.beans.ProcedureBean;
import com.salon.client.beans.WhereBean;
import com.salon.client.beans.WorkDayBean;
import com.salon.server.db.DBUtils;
import com.google.gwt.user.client.rpc.core.java.util.Arrays;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
    GreetingService {
	
	public void init() {
		Parameters.init(getServletContext());
	}
	
	public void login(String username, String password) throws ServiceException {
		DBUtils.getInstance().loadUser(username, password);
	}
	
	@Override
	public void register(String username, String password, String name)
			throws ServiceException {
		DBUtils.getInstance().registerNewUser(username, password, name);
		
	}
	
	public ParentPagingLoadResult<ClientBean> getClients(int offset, int limit, List<WhereBean> wheres) throws ServiceException {
		
		List<ClientBean> resultList = DBUtils.getInstance().loadClients(wheres);
		List<ClientBean> newList = new ArrayList<ClientBean>();
		int i = 0;
		
		for (ClientBean bean : resultList) {
			if (i < offset) {
				i++;
				continue;
			}
			
			newList.add(bean);
			i++;
			if (i >= (offset + limit)) {
				break;
			}
		}
		ParentPagingLoadResult<ClientBean> pagingLoadResult = new ParentPagingLoadResult<ClientBean>();
		pagingLoadResult.setData(newList);
		pagingLoadResult.setOffset(offset);
		pagingLoadResult.setTotalLength((resultList == null) ? 0 : resultList.size());
		
		return pagingLoadResult;		
	}
	
	public void addClient(ClientBean bean) throws ServiceException {
		DBUtils.getInstance().addClient(bean);
	}
	
	public ParentPagingLoadResult<MasterBean> getMasters(int offset, int limit, List<WhereBean> wheres) throws ServiceException {
		
		List<MasterBean> resultList = DBUtils.getInstance().loadMasters(wheres);
		List<MasterBean> newList = new ArrayList<MasterBean>();
		int i = 0;
		
		for (MasterBean bean : resultList) {
			if (i < offset) {
				i++;
				continue;
			}
			
			newList.add(bean);
			i++;
			if (i >= (offset + limit)) {
				break;
			}
		}
		ParentPagingLoadResult<MasterBean> pagingLoadResult = new ParentPagingLoadResult<MasterBean>();
		pagingLoadResult.setData(newList);
		pagingLoadResult.setOffset(offset);
		pagingLoadResult.setTotalLength((resultList == null) ? 0 : resultList.size());
		
		return pagingLoadResult;		
	}
	
	public void addMaster(MasterBean bean) throws ServiceException {
		DBUtils.getInstance().addMaster(bean);
	}
	
	public ParentPagingLoadResult<MaterialBean> getMaterials(int offset, int limit, List<WhereBean> wheres) throws ServiceException {
		
		List<MaterialBean> resultList = DBUtils.getInstance().loadMaterials(wheres);
		List<MaterialBean> newList = new ArrayList<MaterialBean>();
		int i = 0;
		
		for (MaterialBean bean : resultList) {
			if (i < offset) {
				i++;
				continue;
			}
			
			newList.add(bean);
			i++;
			if (i >= (offset + limit)) {
				break;
			}
		}
		ParentPagingLoadResult<MaterialBean> pagingLoadResult = new ParentPagingLoadResult<MaterialBean>();
		pagingLoadResult.setData(newList);
		pagingLoadResult.setOffset(offset);
		pagingLoadResult.setTotalLength((resultList == null) ? 0 : resultList.size());
		
		return pagingLoadResult;		
	}
	
	public void addMaterial(MaterialBean bean) throws ServiceException {
		DBUtils.getInstance().addMaterial(bean);
	}
	
	public void deleteItem(String table, int id) throws ServiceException {
		DBUtils.getInstance().deleteItem(table, id);
	}
	
	public ParentPagingLoadResult<IncomeBean> getIncomes(int offset, int limit) throws ServiceException {
		
		List<IncomeBean> resultList = DBUtils.getInstance().loadIncomes();
		List<IncomeBean> newList = new ArrayList<IncomeBean>();
		int i = 0;
		
		for (IncomeBean bean : resultList) {
			if (i < offset) {
				i++;
				continue;
			}
			
			newList.add(bean);
			i++;
			if (i >= (offset + limit)) {
				break;
			}
		}
		ParentPagingLoadResult<IncomeBean> pagingLoadResult = new ParentPagingLoadResult<IncomeBean>();
		pagingLoadResult.setData(newList);
		pagingLoadResult.setOffset(offset);
		pagingLoadResult.setTotalLength((resultList == null) ? 0 : resultList.size());
		
		return pagingLoadResult;
	}
	
	public IncomeMaterialsBean addIncomeMaterial(IncomeMaterialsBean bean) throws ServiceException {
		return DBUtils.getInstance().addIncomeMaterial(bean);
	}
	
	public void addIncome(IncomeBean bean) throws ServiceException {
		DBUtils.getInstance().addIncome(bean);
	}
	
	public ParentPagingLoadResult<PriceBean> getPrice(int offset, int limit, List<WhereBean> wheres) throws ServiceException {
		
		List<PriceBean> resultList = DBUtils.getInstance().loadPrice(wheres);
		List<PriceBean> newList = new ArrayList<PriceBean>();
		int i = 0;
		
		for (PriceBean bean : resultList) {
			if (i < offset) {
				i++;
				continue;
			}
			
			newList.add(bean);
			i++;
			if (i >= (offset + limit)) {
				break;
			}
		}
		ParentPagingLoadResult<PriceBean> pagingLoadResult = new ParentPagingLoadResult<PriceBean>();
		pagingLoadResult.setData(newList);
		pagingLoadResult.setOffset(offset);
		pagingLoadResult.setTotalLength((resultList == null) ? 0 : resultList.size());
		
		return pagingLoadResult;
	}
	
	public void addPrice(PriceBean bean) throws ServiceException {
		DBUtils.getInstance().addPrice(bean);
	}

	@Override
	public void logout() throws ServiceException {
		// TODO Auto-generated method stub
		
	}
	
	public void updateClient(ClientBean bean) throws ServiceException {
		DBUtils.getInstance().updateClient(bean);
	}
	
	public void updateMaster(MasterBean bean) throws ServiceException {
		DBUtils.getInstance().updateMaster(bean);
	}
	
	public IncomeMaterialsBean addUsedMaterial(IncomeMaterialsBean bean) throws ServiceException {
		return DBUtils.getInstance().addUsedMaterial(bean);
	}
	
	public ProcedureBean addProcedure(ProcedureBean bean) throws ServiceException {
		return DBUtils.getInstance().addProcedure(bean);
	}
	
	public ParentPagingLoadResult<ProcedureBean> loadProcedures(int offset, int limit) throws ServiceException {
		List<ProcedureBean> resultList = DBUtils.getInstance().loadProcedures();
		List<ProcedureBean> newList = new ArrayList<ProcedureBean>();
		int i = 0;
		
		for (ProcedureBean bean : resultList) {
			if (i < offset) {
				i++;
				continue;
			}
			
			newList.add(bean);
			i++;
			if (i >= (offset + limit)) {
				break;
			}
		}
		ParentPagingLoadResult<ProcedureBean> pagingLoadResult = new ParentPagingLoadResult<ProcedureBean>();
		pagingLoadResult.setData(newList);
		pagingLoadResult.setOffset(offset);
		pagingLoadResult.setTotalLength((resultList == null) ? 0 : resultList.size());
		
		return pagingLoadResult;
	}
	
	public List<WorkDayBean> getWorkDays(int year, int month) throws ServiceException {
		return WorkDayManager.getInstance().getWorkDays(year, month);
	}
	
	public void addWorkDay(int masterId, int day, int month, int year) throws ServiceException {
		WorkDayManager.getInstance().addWorkDay(masterId, day, month, year);
	}
	public void deleteWorkDay(int masterId, int day, int month, int year) throws ServiceException {
		WorkDayManager.getInstance().deleteWorkDay(masterId, day, month, year);
	}
	
	public void updateMaterial(MaterialBean bean) throws ServiceException {
		DBUtils.getInstance().updateMaterial(bean);
	}
	
	public Map<Date, Map<MasterBean, ProcedureBean>> getProceduresForDay(Date date, int offset) throws ServiceException {
		return WorkDayManager.getInstance().getProceduresForDay(date, offset);
	}
	
	public ParentPagingLoadResult<IncomeMaterialsBean> loadCurrentMaterials(int offset, int limit, List<WhereBean> wheres)
	throws ServiceException {
		List<IncomeMaterialsBean> resultList = DBUtils.getInstance().loadCurrentMaterials(wheres);
		List<IncomeMaterialsBean> newList = new ArrayList<IncomeMaterialsBean>();
		int i = 0;
		
		for (IncomeMaterialsBean bean : resultList) {
			if (i < offset) {
				i++;
				continue;
			}
			
			newList.add(bean);
			i++;
			if (i >= (offset + limit)) {
				break;
			}
		}
		ParentPagingLoadResult<IncomeMaterialsBean> pagingLoadResult = new ParentPagingLoadResult<IncomeMaterialsBean>();
		pagingLoadResult.setData(newList);
		pagingLoadResult.setOffset(offset);
		pagingLoadResult.setTotalLength((resultList == null) ? 0 : resultList.size());
		
		return pagingLoadResult;
	}
	
	public void calcCurrentMaterial() throws ServiceException {
		DBUtils.getInstance().calcCurrentMaterial();
	}
	
	public void addOutcomeMaterial(IncomeMaterialsBean bean) throws ServiceException {
		DBUtils.getInstance().addOutcomeMaterial(bean);
	}
	
	public ParentPagingLoadResult<IncomeMaterialsBean> getOutcomeMaterials(int offset, int limit, List<WhereBean> wheres)
	throws ServiceException {
		List<IncomeMaterialsBean> resultList = DBUtils.getInstance().loadOutcomeMaterials(wheres);
		List<IncomeMaterialsBean> newList = new ArrayList<IncomeMaterialsBean>();
		int i = 0;
		
		for (IncomeMaterialsBean bean : resultList) {
			if (i < offset) {
				i++;
				continue;
			}
			
			newList.add(bean);
			i++;
			if (i >= (offset + limit)) {
				break;
			}
		}
		ParentPagingLoadResult<IncomeMaterialsBean> pagingLoadResult = new ParentPagingLoadResult<IncomeMaterialsBean>();
		pagingLoadResult.setData(newList);
		pagingLoadResult.setOffset(offset);
		pagingLoadResult.setTotalLength((resultList == null) ? 0 : resultList.size());
		
		return pagingLoadResult;
	}
	
	public void updateProcedure(ProcedureBean bean) throws ServiceException {
		DBUtils.getInstance().updateProcedure(bean);
	}
}
